package org.linuxsogood.boot.misc;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author honway
 * @date 2019/3/5 11:08.
 */
public class TestSort {


    public static void main(String[] args) {

        Semaphore semaphore = new Semaphore(1);
        try {
            System.out.println("init semaphore size 1");
            System.out.println("start acquire...");
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        try {
            semaphore.acquire();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("end");

        int[] arr = new int[]{3, 44, 38, 5, 47, 15, 3, 36, 26, 1, 27, 2, 46, 4, 19, 50, 48};
        // int[] arr = new int[]{3, 44, 1, 27, 2, 48};
        fastSort(arr);
        System.out.println(Arrays.toString(arr));


        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);

        String join = StringUtils.join(list, ",", "(");
        System.out.println(join);
        AtomicInteger atomicInteger = new AtomicInteger();
        atomicInteger.incrementAndGet();
    }

    private static void fastSort(int[] arr) {
        fastSort(arr, 0, arr.length -1);

    }

    private static void fastSort(int[] arr, int start, int end) {
        // 查找基准的位置
        int baseIndex = partition(arr, start, end);
        if (baseIndex > start + 1) {
            fastSort(arr, start, baseIndex - 1);
        }
        if (baseIndex < end - 1) {
            fastSort(arr, baseIndex + 1, end);
        }
    }

    private static int partition(int[] arr, int start, int end) {
        int base = arr[start];
        while (start < end) {
            while (start < end && arr[end] >= base) {
                end--;
            }
            if (start < end) {
                arr[start] = arr[end];
                start++;
            }

            while (start < end && arr[start] <= base) {
                start++;
            }
            if (start < end) {
                arr[end] = arr[start];
                end--;
            }
        }
        arr[start] = base;
        return start;
    }

    private static void selectSort(int[] arr) {
        boolean changeFlag = false;
        for (int i = 0; i < arr.length - 1; i++) {
            int minIdx = i;
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIdx]) {
                    minIdx = j;
                    changeFlag = true;
                }
            }
            if (changeFlag) {
                int temp = arr[i];
                arr[i] = arr[minIdx];
                arr[minIdx] = temp;
                changeFlag = false;
            }
        }
    }

    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));
    }
}
